package com.jinlingxiu.app.mvp.ui.activity

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.KeyEvent
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.TimeUtils
import com.jaeger.library.StatusBarUtil
import com.jinlingxiu.app.R
import com.jinlingxiu.app.mvp.contract.SolutionContract
import com.jinlingxiu.app.mvp.presenter.SolutionPresenter
import com.jinlingxiu.app.other.adapter.SolutionAdapter
import com.jinlingxiu.app.other.bean.ErrorBean
import com.jinlingxiu.app.other.bean.SolutionBean
import com.jinlingxiu.app.other.db.entity.UserInfoBean
import com.xdja.easymvp.base.BaseActivity
import kotlinx.android.synthetic.main.activity_solution.*
import org.jetbrains.anko.toast
import org.koin.androidx.scope.lifecycleScope


/**
 * 描述:  解决方案页
 * Create by yuanwanli
 * Date 2020/09/11
 */
class SolutionActivity : BaseActivity<SolutionPresenter>(), SolutionContract.View {
    override var mPresenter: SolutionPresenter? = lifecycleScope.get()
    var errors: ArrayList<ErrorBean>? = null
    lateinit var solutionAdapter: SolutionAdapter
    private var firstTime: Long = 0

    override fun initView(savedInstanceState: Bundle?): Int = R.layout.activity_solution

    override fun initData(savedInstanceState: Bundle?) {
        StatusBarUtil.setTransparent(this)
        StatusBarUtil.setLightMode(this)
        ivBack.setOnClickListener {
            finish()
        }
        ivSave.setOnClickListener {
            mPresenter?.saveScreen(this)
        }
        mPresenter?.getUserInfo()
        errors = intent.getParcelableArrayListExtra("data")
        solutionAdapter = SolutionAdapter(mRecyclerView)
        mPresenter?.transformData(errors)
    }

    @SuppressLint("SetTextI18n")
    override fun showUser(userInfoBean: UserInfoBean) {
        tvTitle.text = String.format(getString(R.string.solution_title), userInfoBean.name)
        tvUnitName.text =
            userInfoBean.unitName + "\n" + TimeUtils.getNowString(TimeUtils.getSafeDateFormat("yyyy年MM月dd日"))
    }

    override fun showAdapterData(list: List<SolutionBean>) {
        solutionAdapter.data = list
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        val secondTime = System.currentTimeMillis()
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (secondTime - firstTime < 2000) {
                AppUtils.exitApp()
            } else {
                toast("再按一次退出程序")
                firstTime = System.currentTimeMillis()
            }
            return true
        }
        return super.onKeyDown(keyCode, event)
    }
}
